package 统计算法.调整数组顺序使奇数位于偶数前面;

import java.util.Arrays;

public class tzszsxsjswyosqm {

    public static void main(String[] args) {
//      int[] nums = {1,2,3,4};
      int[] nums = {2,16,3,5,13,1,16,1,12,18,11,8,11,11,5,1};
      System.out.println(Arrays.toString(exchange(nums)));
    }

    public static int[] exchange(int[] nums) {
        int left = 0;
        int right = nums.length - 1;

        while (left < right){
            System.out.println(Arrays.toString(nums));
            if ((nums[left] & 1) == 0){
                if ((nums[right] & 1) == 0){
                    --right;
                }else {
                    nums[left] = nums[left] + nums[right];
                    nums[right] = nums[left] - nums[right];
                    nums[left] = nums[left] - nums[right];
                    ++left;
                }

            }else if ((nums[left] & 1) == 1){
                ++left;
            }
            if ((nums[right] & 1) == 0){
                --right;
            }
        }
        return nums;
    }
}
